package com.drojian.operator;

import org.junit.Test;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;

public class FilterOperator {
    @Test
    public void Test1() {
        System.out.println("======================");
        FilterOperator operator = new FilterOperator();
        operator.Test2();
        System.out.println("======================");
    }

    Observer observer = new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
            System.out.println("onSubscribe:" + d + Thread.currentThread());
        }

        @Override
        public void onNext(Object o) {
            System.out.println("onNext:" + o + Thread.currentThread());
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("onError:" + e + Thread.currentThread());
        }

        @Override
        public void onComplete() {
            System.out.println("onComplete:" + Thread.currentThread());
        }
    };

    public void Test2() {
        //range 本来是1-10
        Observable.range(1, 10)
                //筛选出大于5的  只保留小于5的
                .filter(new Predicate<Integer>() {

                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer < 5;
                    }
                })
                .subscribe(observer);

    }
}
